package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/truncate-sentence/">1816.截断句子</a>
 */
public class Solution1816 extends BaseSolution {

    public String truncateSentence(String s, int k) {
        int p = 0;
        int sc = 0;
        int len = s.length();
        while (p < len) {
            char c = s.charAt(p++);
            if (c == ' ' && ++sc >= k) {
                p--;
                break;
            }
        }
        return s.substring(0, p);
    }

    public static void main(String[] args) {
        Solution1816 solution = new Solution1816();
        System.out.println(solution.truncateSentence("Hello how are you Contestant", 10));
    }
}